package jp.sourceforge.akjrcp.util;

public class TimeUtils {
	
	/*
	public static String toMillisecondLabel(long millisecond){
		long hour_millisecond=(60*60*1000);
		int minute_millisecond=60*1000;
		int second_millisecond=1000;
		int hour=(int)(millisecond/hour_millisecond);
		long remain=millisecond%hour_millisecond;
		int minute=(int)(remain/minute_millisecond);
		remain=remain%minute_millisecond;
		int second=(int)(remain/second_millisecond);
		remain=remain%second_millisecond;
		
		if(hour>0){
		return toLabel2(hour)+":"+toLabel2(minute)+":"+toLabel2(second)+"."+remain;
		}else{
		return toLabel2(minute)+":"+toLabel2(second)+"."+toLabel3((int) remain);	
		}
}*/
	//public static long to
	/**
	 * @return default 00:00:00.000
	 */
	public static String toTimeText(long millisecond){
		return toTimeText(millisecond,true,true,true,true);
		
	}
	
	public static long toMillisecond(int hour,int minute,int second,int millisecond){
		long ret=0;
		ret+=hour*60*60*1000;
		ret+=minute*60*1000;
		ret+=second*1000;
		ret+=millisecond;
		return ret;
	}
	
	public static long toMillisecond(String text){
		int vs[]=toHourMinSecMilliSecond(text);
		if(vs==null){
			return 0;
		}else{
			return toMillisecond(vs[0],vs[1],vs[2],vs[3]);
		}
	}
	
	/**
	 *@return (hour,min,sec,millisecond)
	 */
	public static int[] toHourMinSecMilliSecond(String text){
		int ret[]=new int[4];
		String values[]=text.split(":");
		if(values.length>3){
			return null; //maximum 3(00:00:00)
		}
		//log.info(values.length+","+values[0]);
		//10.24h;
		
		if(values[values.length-1].indexOf(".")!=-1){
			String sec_milli[]=values[values.length-1].split("\\.");
			//log.info(sec_milli.length);
			//log.info(sec_milli[0]+","+sec_milli[1]);
			if(sec_milli.length!=2){
				return null;
				}else{
					if(isDigitOnly(sec_milli[0]) && isDigitOnly(sec_milli[1])){
						values[values.length-1]=sec_milli[0];
						ret[3]=Integer.parseInt(sec_milli[1]);//set millisecond
					}else{
						return null;
					}
				}
			}
		
		if(values.length==1){
			//have millisecond; 10.24
			//check 24h 60m 30s 10
			String tValues[]={"h","m","s"};
			String checkValue=values[0];
			String ends="";
			for (int i = 0; i < tValues.length; i++) {
			if(checkValue.toLowerCase().endsWith(tValues[i])){
				checkValue=checkValue.substring(0,checkValue.length()-1);
				ends=tValues[i];
				break;
				}
			}
			
			if(isDigitOnly(checkValue)){
				if(ends.equals("h")){
					ret[0]=Integer.parseInt(checkValue);
				}else if(ends.equals("m")){
					ret[1]=Integer.parseInt(checkValue);
				}else{//s or none
					ret[2]=Integer.parseInt(checkValue);
				}
				return ret;
			}else{
				return null;
			}
		}
		
		for (int i = 0; i < values.length; i++) {
			if(!isDigitOnly(values[i])){
				return null;
			}else{
				ret[i]=Integer.parseInt(values[i]);
			}
		}
		
		return ret;
	}
	public static boolean isTimeText(String text){
		return toHourMinSecMilliSecond(text)!=null;
	}
	
	public static boolean isDigitOnly(String text){
		for (int i = 0; i < text.length(); i++) {
			if(!Character.isDigit(text.charAt(i))){
				return false;
			}
		}
		return true;
	}
	
		public static String toTimeText(long millisecond,boolean showHour,boolean showMinute,boolean showSecond,boolean showMilliSecond){
		long hour_millisecond=(60*60*1000);
		int minute_millisecond=60*1000;
		int second_millisecond=1000;
		int hour=(int)(millisecond/hour_millisecond);
		long remain=millisecond%hour_millisecond;
		int minute=(int)(remain/minute_millisecond);
		remain=remain%minute_millisecond;
		int second=(int)(remain/second_millisecond);
		remain=remain%second_millisecond;
		
		String ret="";
		if(showHour){
			ret+=toLabel2(hour)+":";
			if(!showMinute){
				ret+="00:";
				if(!showSecond){
					ret+="00";
				}
			}
			
		}
		
		if(showMinute){
			ret+=toLabel2(minute)+":";
			if(!showSecond){
				ret+="00";
			}
		}
		
		if(showSecond){
			ret+=toLabel2(second);
		}
		
		if(showMilliSecond){
			ret+=":";
			ret+=toLabel3((int) remain);
		}
		
		return ret;
}
	
	private static String toLabel2(int number){
		String v=""+number;
		if(v.length()<2)
		v="0"+v;
		return v;
	}
	

	private static String toLabel3(int number){
		String v=""+number;
		if(v.length()<2)
		v="0"+v;
		if(v.length()<3)
		v="0"+v;
		if(v.length()>3)
		return "000";	//error
		return v;
	}
}
